package com.xg.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xg.common.constant.TokenConstant;
import com.xg.common.request.OrderItemDTO;
import com.xg.common.response.TokenResponse;
import com.xg.common.util.Func;
import com.xg.common.util.JWTUtils;
import com.xg.entity.UsedCar;
import com.xg.entity.UsedCarBrand;
import com.xg.entity.UsedCarSeries;
import com.xg.service.IUsedCarBrandService;
import com.xg.service.IUsedCarSeriesService;
import com.xg.service.IUsedCarService;
import com.xg.vo.UsedCarVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jdh
 * @since 2023-03-30
 */
@Controller
@RequestMapping("/usedCar")
public class UsedCarController {
    
    @Autowired
    private IUsedCarService iUsedCarService;
    @Autowired
    private IUsedCarBrandService iUsedCarBrandService;
    @Autowired
    private IUsedCarSeriesService iUsedCarSeriesService;
    
    /**
     * @Author 罗迎庆
     * @Methodname queryUsedCarList       
     * @Description //TODO (查询汽车详情)
     * @Date 22:17 2023/3/31
     * @Param []
     * @return com.xg.common.result.ApiResult<com.xg.entity.UsedCar>
     **/
    @RequestMapping("/queryUsedCarList")
    public String queryUsedCarList(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,Model model, String usedCarName,Integer orderId,Integer minPrice,Integer maxPrice) {
        PageHelper.startPage(pageNum,10);
        List<UsedCar> carList = iUsedCarService.queryUsedCarList(orderId,usedCarName,minPrice,maxPrice);
        PageInfo<UsedCar> pageInfo=new PageInfo<>(carList);
        List<UsedCarBrand> brandList = iUsedCarBrandService.queryUsedCarBrandList();
        model.addAttribute("brandList",brandList);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("carList",carList);
        return "PurchaseCar";
    }


    /**
     * @description: TODO (汽车详情信息查询)
     * @Methodname findCarList
     * @author 荣晓凯
     * @date 2023/3/31 9:36
     * @version 1.0
     * @return "details/carShow"
     */
    @RequestMapping("findCarList")
    public String findCarList(HttpServletRequest request,@RequestParam("usedCarId") Integer  usedCarId){
        UsedCarVO usedCarVO = iUsedCarService.findCarList(usedCarId);
        request.setAttribute("vo",usedCarVO);
        System.out.println(usedCarVO);
        return "details/carShow";
    }

    /**
     * @Author 罗迎庆
     * @Methodname home       
     * @Description //TODO (汽车详情信息查询一对一)
     * @Date 15:38 2023/4/6
     * @Param []
     * @return java.lang.String
     **/
    @RequestMapping("/queryCarList")
    public String queryCarList(Model model,@RequestParam("usedCarId") Integer usedCarId){
        List<UsedCar> list = iUsedCarService.queryCarList(usedCarId);
        model.addAttribute("queryCarList",list);
        return "PurchaseCar";
    }



    @RequestMapping("/home")
    public String home(){
        return "PurchaseCar";
    }

    /**
     * @description: TODO(我要卖车)
     * @author 荣晓凯
     * @date 2023/4/3 21:21
     * @version 1.0
     */
    @RequestMapping("details")
    public String details(){
        return "details/sellCar";
    }

    /**
     * @description(隐私权条款)
     * @param: null
     * @return:
     * @author 荣晓凯
     * @date: 2023/4/6 14:43
     */
    @RequestMapping("clause")
    public String clause(){
        return "details/clause";
    }


    @RequestMapping("queryUsedCarSearchList")
    public String queryUsedCarSearchList(Model model, UsedCar usedCar) {
        List<UsedCar> list = iUsedCarService.queryUsedCarSearchList(usedCar);
        model.addAttribute("searchList", list);
        return "index";
    }

    /*
    * 查询页面信息并跳转到主页面
    * */
    @RequestMapping("queryUsedCarListHome")
    public String queryUsedCarListHome(Model model, HttpServletRequest request) {
        // 获取token
        String token = "";
        Cookie[] cookies = request.getCookies();
        if (!Func.isEmpty(cookies)){
            for (Cookie cookie : cookies) {
                String successTokenKey = TokenConstant.SUCCESS_TOKEN;
                if (null != cookie && successTokenKey.equals(cookie.getName())){
                    token = cookie.getValue();
                    break;
                }
            }
        }
        if (!Func.isEmpty(token)){
            //jwt解密
            TokenResponse tokenResponse = JWTUtils.checkToken(token);
            System.out.println(tokenResponse.getUsername());
            System.out.println(tokenResponse.getUserPhone());
            //返回用户信息
            model.addAttribute("userName",tokenResponse.getUsername());
            model.addAttribute("userPhone",tokenResponse.getUserPhone());
        }

        List<UsedCar> carListWithImages = iUsedCarService.queryUsedCarListWithImages();
        //将查询到的主表与图片信息，流化并且限制8条数据，然后转化为list集合
        List<UsedCar> imagesCollect = carListWithImages.stream().limit(8).collect(Collectors.toList());
        List<UsedCar> statusList = iUsedCarService.queryImagesWithStatus();
        List<UsedCar> statusCollect = statusList.stream().limit(8).collect(Collectors.toList());
        List<UsedCar> energyList = iUsedCarService.queryImagesWithEnergy();
        List<UsedCar> energyCollect = energyList.stream().limit(8).collect(Collectors.toList());
        List<UsedCar> typeList = iUsedCarService.queryImagesWithType();
        List<UsedCar> typeCollect = typeList.stream().limit(8).collect(Collectors.toList());
        List<UsedCarBrand> brandList = iUsedCarBrandService.queryUsedCarBrandList();
        List<UsedCarBrand> brandCollect = brandList.stream().limit(6).collect(Collectors.toList());
        List<UsedCarSeries> seriesList = iUsedCarSeriesService.queryUsedCarSeriesListTwo();
        List<UsedCarSeries> seriesCollect = seriesList.stream().limit(6).collect(Collectors.toList());
        model.addAttribute("brandList",brandCollect);
        model.addAttribute("seriesList",seriesCollect);
        model.addAttribute("carListImages",imagesCollect);
        model.addAttribute("statusList",statusCollect);
        model.addAttribute("energyList",energyCollect);
        model.addAttribute("typeList",typeCollect);
        return "index";
    }

    /**
     * 根据carId查询车辆详情
     * @param carId
     * @return
     */
    @GetMapping("/carInfo")
    @ResponseBody
    public OrderItemDTO getCarByCarId(@RequestParam("carId") Long carId){
        return iUsedCarService.getCarByCarId(carId);
    }

    /**
     * @description: 卖车咨询发送短信
     * @author 荣晓凯
     * @date 2023/4/3 17:22
     * @version 1.0
     */
    @RequestMapping("/sendMsg")
    @ResponseBody
    public void  sendMsg(String phone){
         iUsedCarService.sendMsg(phone);
        System.out.println(phone+"asdsadsadasdasadhasfhakgfukdsfsdufsdufgsdgkfhsdhfsdkfhksd");
        iUsedCarService.sendMsg(phone);
    }

    /**
     * 更新车辆状态
     * @param carId
     */
    @GetMapping("/updateUsedCar")
    @ResponseBody
    public void updateUsedCar(@RequestParam("carId")Long carId){
        iUsedCarService.updateUsedCar(Integer.parseInt(String.valueOf(carId)));
    }

}
